FIx method resolution in number slots when a single method is a subclass#6228
Open
MatthieuDartiailh wants to merge 7 commits into
Open
FIx method resolution in number slots when a single method is a subclass#6228MatthieuDartiailh wants to merge 7 commits into
MatthieuDartiailh wants to merge 7 commits into
Conversation
… class when necessary
…class implement a single method
MatthieuDartiailh
force-pushed
the
binary-op-fix
branch
from
July 24, 2026 17:00
811e8ea to
9f19128
Compare
chirizxc
reviewed
Jul 24, 2026
chirizxc
reviewed
Jul 24, 2026
Comment on lines
+524
to
+532
| // ============================================================================ | ||
| // Binary Operator Inheritance Tests | ||
| // Tests super() delegation with parent classes and multiple inheritance | ||
| // ============================================================================ | ||
|
|
||
| /// Test 1: Parent Forward → Child Reflected (Rust only) | ||
| /// Parent implements __add__, child implements __radd__ | ||
| #[pyclass(subclass)] | ||
| struct AddBase; |
Contributor
There was a problem hiding this comment.
I think it's best to delete these AI comments as well (looks verbose)
Contributor
Author
There was a problem hiding this comment.
Those were AI generated but at my demand to make it easier to navigate the tests (there are a lot and it is not always straightforward to figure out what they test). But I am fine removing them.
chirizxc
reviewed
Jul 24, 2026
chirizxc
reviewed
Jul 24, 2026
|
|
||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| return unsafe { collector.__pow__(py, _slf, _other, _mod) }; |
Contributor
There was a problem hiding this comment.
Here, we can add a fast path so that direct type object comparisons avoid the creation of a Bound:
let lhs_type = unsafe { (*_slf).ob_type };
let type_ = <$cls as $crate::PyTypeInfo>::type_object_raw(py);
if lhs_type == type_ {
return unsafe { collector.$lhs(py, _slf, _other) };
}
chirizxc
reviewed
Jul 24, 2026
| }}; | ||
|
|
||
| // Only forward available | ||
| ($cls:ty, true, false) => {{ |
Contributor
There was a problem hiding this comment.
Same type optimization as above:
let collector = PyClassImplCollector::<$cls>::new();
- let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) };
- if lhs_obj.is_instance_of::<$cls>() {
+ let lhs_type = unsafe { (*_slf).ob_type };
+ let type_ = <$cls as $crate::PyTypeInfo>::type_object_raw(py);
+ if lhs_type == type_ || unsafe {
+ $crate::Bound::from_borrowed_ptr(py, _slf).is_instance_of::<$cls>()
+ } {
return unsafe { collector.$lhs(py, _slf, _other) };
}
- let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) };
- if rhs_obj.is_instance_of::<$cls>() {
+ let rhs_type = unsafe { (*_other).ob_type };
+ if rhs_type == type_ || unsafe {
+ $crate::Bound::from_borrowed_ptr(py, _other).is_instance_of::<$cls>()
+ } {
return unsafe { collector.$rhs(py, _other, _slf) };
}Expand test_arithmetics to cover this case
MatthieuDartiailh
marked this pull request as ready for review
July 24, 2026 22:37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #6024
When a class implement both half of a slot (
__add__and__radd__) we use the old dispatch strategy except that we dispatch to the appropriate according to the type of the arguments. In other cases, we use a super object to support multiple inheritance with pure Python classes.